package com.blockmeta.bbs.minemodule.activity;

import android.content.Context;
import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blockmeta.bbs.baselibrary.net.callback.Callback;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterConstant;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.net.okhttp.OKHttpGetReq;
import com.blockmeta.bbs.businesslibrary.net.pojo.ForumPostContentBean;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.minemodule.R;
import com.blockmeta.bbs.minemodule.R2;
import com.blockmeta.bbs.minemodule.adapter.MyPostsAdapter;
import com.blockmeta.bbs.minemodule.pojo.MyPostsPOJO;
import com.blockmeta.bbs.minemodule.pojo.sort.SortByDate;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Created by author on 2018/4/19.
 */

@Route(path = RouterPath.MY_FORUMPOST_ACTIVITY,extras = RouterConstant.EXTRAL_LOGIN)
public class MyPostsActivity extends BaseActivity implements OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R2.id.list_recy)
    RecyclerView list_recy;
    @BindView(R2.id.list_swipe)
    SmartRefreshLayout list_swipe;
    private Context mContext;
    private MyPostsAdapter adapter;
    private OKHttpGetReq okHttpGetReq;
    private MyPostsPOJO mPOJO;
    private int pageNo = 2;

    @Autowired(name = Constant.ZERO)
    public boolean mIsChoose;

    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public boolean showRight() {
        return false;
    }

    @Override
    public void toDoRightSomething() {
        if(adapter.getCheckedItems().size() == 0){
            ToastUtil.show("请先选择一个");
            return;
        }

        MyPostsPOJO.ListBean listBean = adapter.getCheckedItems().get(0);
        ForumPostContentBean postContentBean = new ForumPostContentBean();
        postContentBean.setId(listBean.getTid());
        postContentBean.setTitle(listBean.getTitle());

        Intent intent = new Intent();
        intent.putExtra("content", postContentBean.toJsonString());
        intent.putExtra("type", 4);


        setResult(200, intent);
        finish();

    }

    @Override
    public int showRightImg() {
        return R.drawable.title_bar_compose;
    }

    @Override
    public View setView() {
        mContext = MyPostsActivity.this;
        setTitleText("我的主题");
        return LayoutInflater.from(mContext).inflate(R.layout.fragment_list_smart, null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        list_swipe.setOnRefreshListener(this);
        list_recy.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        adapter = new MyPostsAdapter(R.layout.item_bbs_list_item, null, mContext);
        adapter.setEditable(mIsChoose, true);
        okHttpGetReq = new OKHttpGetReq(mContext, NetConstant.USER_BBS_POST+"?authorid="+AccountHelper.getUid());
        initNet();
    }

    private void initNet() {
        okHttpGetReq.getDataWithParam(new Callback() {
            @Override
            public void onError(String resp) {
                processErrorData();
            }

            @Override
            public void onResponse(String resp, int id) {
                processData(resp);
            }
        }, "uid", AccountHelper.getUid());
    }

    private void processErrorData() {
        if(mUnbinder == null||list_recy==null) return;
        ProgressBarUtil.dismissProgress(mContext);
        View errorView = LayoutInflater.from(mContext).inflate(R.layout.empty_net, null);
        ImageView reconn = (ImageView) errorView.findViewById(R.id.click_reconn);
        reconn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initNet();
            }
        });
        adapter.setEmptyView(errorView);
        list_recy.setAdapter(adapter);
    }

    private void processData(String resp) {
        if(mUnbinder == null) return;
        try {
            mPOJO = GsonUtil.buildGson().fromJson(resp, MyPostsPOJO.class);
        } catch (Exception e) {
        }
        list_recy.setAdapter(adapter);
        adapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.empty_data, null));
        if (mPOJO == null) return;
        List<MyPostsPOJO.ListBean> listBeans = mPOJO.getList();
        Collections.sort(listBeans,new SortByDate<MyPostsPOJO.ListBean>());
        adapter.setNewData(listBeans);

        adapter.setOnLoadMoreListener(this, list_recy);

    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        pageNo = 2;
        okHttpGetReq.getDataWithParam(new Callback() {
            private MyPostsPOJO refreshpojo;

            @Override
            public void onError(String resp) {
                list_swipe.finishRefresh(true);
            }

            @Override
            public void onResponse(String resp, int id) {
                list_swipe.finishRefresh(true);

                try {
                    refreshpojo = GsonUtil.buildGson().fromJson(resp, MyPostsPOJO.class);
                } catch (Exception e) {
                }
                if (refreshpojo == null) return;
                adapter.setNewData(refreshpojo.getList());

            }
        }, "uid", AccountHelper.getUid());
    }

    @Override
    public void onLoadMoreRequested() {
        HashMap<String, String> map = new HashMap<>();
        map.put("page", pageNo + "");
        okHttpGetReq.getDataWithParam(new Callback() {
            private MyPostsPOJO loadMore;

            @Override
            public void onError(String resp) {
                adapter.loadMoreEnd(true);
            }

            @Override
            public void onResponse(String resp, int id) {
                list_swipe.finishRefresh(true);
                try {
                    loadMore = GsonUtil.buildGson().fromJson(resp, MyPostsPOJO.class);
                } catch (Exception e) {
                    adapter.loadMoreEnd(true);
                }
                if (loadMore == null || loadMore.getList() == null) {
                    adapter.loadMoreEnd(true);
                    return;
                }
                adapter.addData(loadMore.getList());
                adapter.loadMoreComplete();
                pageNo++;
            }
        }, "uid", AccountHelper.getUid(), map);
    }
}
